package com.masterclient.app.modules.income;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.base.FragmentHostActivity;
import com.masterclient.app.modules.income.adapter.UnfreezeAdapter;
import com.masterclient.app.modules.income.contract.UnFreezeContract;
import com.masterclient.app.modules.income.contract.UnFreezePresenter;
import com.masterclient.app.modules.income.model.UnFreezeBean;
import com.eoner.uikit.header.MyRefreshHead;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

import static com.masterclient.app.modules.income.WithdrawDetailsFragment.PAGE_TYPE;
import static com.masterclient.app.modules.income.WithdrawDetailsFragment.UNFREEZE_DETAILS;

@Route(path = AroutePath.UNFREEZE_LIST_FRAGMENT)
public class UnfreezeListFragment extends BaseFragment<UnFreezePresenter> implements UnFreezeContract.View {

    @BindView(R.id.view_title)
    View viewTitle;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.view_empty)
    View emptyView;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.iv_empty)
    ImageView ivEmpty;

    private View footView;
    private int page = 1;
    private boolean hasMore;
    private String bizType = "0";

    private OnRefreshSuccessListener onRefreshSuccessListener;
    private UnfreezeAdapter adapter;
    private List<UnFreezeBean.UnFreezeItemBean> list = new ArrayList<>();

    public static UnfreezeListFragment newInstance(String bizType) {
        UnfreezeListFragment unfreezeListFragment = new UnfreezeListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("bizType", bizType);
        unfreezeListFragment.setArguments(bundle);
        return unfreezeListFragment;
    }

    public void setOnRefreshSuccessListener(OnRefreshSuccessListener onRefreshSuccessListener){
        this.onRefreshSuccessListener = onRefreshSuccessListener;
    }

    interface OnRefreshSuccessListener{
        void getFrozenTotalTitle(String frozenTotalTitle);
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_un_clear_account, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();

        if (getArguments() != null){
            bizType = getArguments().getString("bizType");
        }

        viewTitle.setVisibility(View.GONE);
        refreshLayout.setRefreshHeader(new MyRefreshHead(getActivity()));
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getUnFreezeList(bizType,String.valueOf(page));
        });
        adapter = new UnfreezeAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter.setOnItemClickListener((adapter, view, position) -> {
            Fragment fragment = (Fragment) ARouter.getInstance().build(AroutePath.WITHDRAW_DETAILS_FRAGMENT)
                    .withString(PAGE_TYPE,UNFREEZE_DETAILS)
                    .withString("frozen_id",list.get(position).frozenId)
                    .withString("frozen_status",list.get(position).bizType)
                    .navigation();
            FragmentHostActivity.openFragment(getActivity(), fragment);
        });
        adapter.bindToRecyclerView(recyclerView);

        footView = View.inflate(mContext, R.layout.view_foot_text, null);
        footView.setVisibility(View.GONE);
        View viewFooter = footView.findViewById(R.id.view_footer);
        viewFooter.setBackgroundColor(Color.parseColor("#00000000"));
        adapter.setFooterView(footView);
        adapter.setOnLoadMoreListener(() -> {
            if (hasMore) {
                page++;
                mPresenter.getUnFreezeList(bizType, String.valueOf(page));
            }
        }, recyclerView);
        mPresenter.getUnFreezeList(bizType,String.valueOf(page));
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @OnClick({R.id.back})
    void onClick(View view) {
        if (view.getId() == R.id.back) {
            Objects.requireNonNull(getActivity()).finish();
        }
    }

    @Override
    public void showData(UnFreezeBean dataBean) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
        }

        if (!TextUtils.isEmpty(dataBean.frozenTotalTitle)){
            onRefreshSuccessListener.getFrozenTotalTitle(dataBean.frozenTotalTitle);
        }else {
            onRefreshSuccessListener.getFrozenTotalTitle("");
        }
        setEmptyStatus(3);
        hasMore = dataBean.hasMore;
        if (list == null) {
            list = new ArrayList<>();
        }
        if (page == 1) {
            list.clear();
        }
        list.addAll(dataBean.list);
        adapter.setNewData(list);

        if (list.size() <= 0) {
            setEmptyStatus(1);
        }else {
            if (!hasMore) {
                adapter.setEnableLoadMore(false);
                adapter.loadMoreEnd();
                footView.setVisibility(View.VISIBLE);
            } else {
                adapter.loadMoreComplete();
                footView.setVisibility(View.GONE);
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showDataFailed(String msg) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh(false);
        }
        ToastUtil.showToast(msg);
    }

    /**
     * 设置页面置空状态
     *
     * @param status status == 1：数据返回为空
     */
    private void setEmptyStatus(int status) {
        if (status == 1) {
            emptyView.setVisibility(View.VISIBLE);
            tvTip.setText("还没有明细呢～");
            tvTip.setTextColor(Color.parseColor("#F23051"));
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvTip.getLayoutParams();
            params.topMargin = ScreenUtils.dp2px(24);
            tvTip.setLayoutParams(params);
            ivEmpty.setImageResource(R.mipmap.icon_null);
        } else {
            emptyView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
